package com.tbit.main.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.tbit.main.constant.AccountDayStatConstant;
import com.tbit.main.constant.DaoParamConstant;
import com.tbit.main.dao.stat.AccountDayStatDao;
import com.tbit.main.dao.stat.RideOrderDayStaDao;
import com.tbit.main.pojo.AccountDayStat;
import com.tbit.main.pojo.RideOrderDaySta;
import com.tbit.main.service.RideOrderDayStaService;
import com.tbit.main.util.DateTimeUtil;
import com.tbit.main.util.StringUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author LMY
 * @create 2021-01-11 20:26
 * 骑行订单日统计
 */
@Service("rideOrderDayStaService")
public class RideOrderDayStaServiceImpl implements RideOrderDayStaService {

    @Autowired
    private RideOrderDayStaDao rideOrderDayStaDao;
    @Autowired
    private AccountDayStatDao accountDayStatDao;

    @Override
    public List<RideOrderDaySta> getByAccountIdDateDay(List<Integer> accountIds, List<Integer> regionId, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        map.put("regionId", StringUtil.getListString(regionId));
        map.put("startDate", DateTimeUtil.getYYMMDDInteger(startDate));
        map.put("endDate", DateTimeUtil.getYYMMDDInteger(endDate));
        List<Integer> dataTypes = new LinkedList<>();
        dataTypes.add(AccountDayStatConstant.CYCLING_INCOME);
        dataTypes.add(AccountDayStatConstant.USER_FINE_INCOME);
        dataTypes.add(AccountDayStatConstant.TOTAL_NUMBER_OF_VEHICLES);
        map.put("dataType", StringUtil.getListString(dataTypes));
        List<RideOrderDaySta> rideOrderDayStaList = new LinkedList<>();
        if (CollUtil.isNotEmpty(accountIds)) {
            List<AccountDayStat> accountDayStats = accountDayStatDao.getByAccountIdDateDay(map);

            List<AccountDayStat> accountDayStatRide = new LinkedList<>();
            Map<String, AccountDayStat> accountDayStatMainMap = new HashMap<>();
            Map<String, AccountDayStat> accountDayStatFineMap = new HashMap<>();
            for (AccountDayStat accountDayStat : accountDayStats) {
                if (accountDayStat.getDataType().equals(AccountDayStatConstant.CYCLING_INCOME)) {
                    accountDayStatRide.add(accountDayStat);
                } else if (accountDayStat.getDataType().equals(AccountDayStatConstant.TOTAL_NUMBER_OF_VEHICLES)) {
                    accountDayStatMainMap.put(accountDayStat.getAccountId() + "_" + DateTimeUtil.getYYMMDDString(String.valueOf(accountDayStat.getDataDate())), accountDayStat);
                } else if (accountDayStat.getDataType().equals(AccountDayStatConstant.USER_FINE_INCOME)) {
                    accountDayStatFineMap.put(accountDayStat.getAccountId() + "_" + DateTimeUtil.getYYMMDDString(String.valueOf(accountDayStat.getDataDate())), accountDayStat);
                }
            }
            rideOrderDayStaList = accountDayStatRide.stream().map(l -> new RideOrderDaySta(l.getAccountId(), l.getRegionId(), l.getCntTotal(), l.getSumTotal(), DateTimeUtil.getYYMMDDString(String.valueOf(l.getDataDate())), l.getAddTime(), 0, 0, l.getDateWeek(), l.getDateMonth(), null, null, null)).collect(Collectors.toList());
            for (RideOrderDaySta rideOrderDaySta : rideOrderDayStaList) {

                AccountDayStat accountDayStatMain = accountDayStatMainMap.get(rideOrderDaySta.getAccountId() + "_" + rideOrderDaySta.getOrderDate());
                if (accountDayStatMain != null) {
                    rideOrderDaySta.setMachineCount(accountDayStatMain.getSumTotal());
                } else {
                    rideOrderDaySta.setMachineCount(0L);
                }
                AccountDayStat accountDayStatFine = accountDayStatFineMap.get(rideOrderDaySta.getAccountId() + "_" + rideOrderDaySta.getOrderDate());
                if (accountDayStatFine != null) {
                    rideOrderDaySta.setOrderMoney(rideOrderDaySta.getOrderMoney() + accountDayStatFine.getSumTotal());
                } else {
                    rideOrderDaySta.setOrderMoney(rideOrderDaySta.getOrderMoney());
                }
            }
        }
        if (CollUtil.isNotEmpty(regionId)) {
            List<AccountDayStat> accountDayStats = accountDayStatDao.getByRegionIdDateDay(map);
            rideOrderDayStaList.addAll(accountDayStats.stream().map(l -> new RideOrderDaySta(l.getAccountId(), l.getRegionId(), l.getCntTotal(), l.getSumTotal(), DateTimeUtil.getYYMMDDString(String.valueOf(l.getDataDate())), l.getAddTime(), 0, 0, l.getDateWeek(), l.getDateMonth(), null, null, null)).collect(Collectors.toList()));
        }
        return rideOrderDayStaList;
    }

    @Override
    public List<RideOrderDaySta> getByAccountIdDateWeek(List<Integer> accountIds, List<Integer> regionId, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        map.put("regionId", StringUtil.getListString(regionId));
        map.put("startDate", DateTimeUtil.getYYMMDDInteger(startDate));
        map.put("endDate", DateTimeUtil.getYYMMDDInteger(endDate));
        List<Integer> dataTypes = new LinkedList<>();
        dataTypes.add(AccountDayStatConstant.CYCLING_INCOME);
        dataTypes.add(AccountDayStatConstant.TOTAL_NUMBER_OF_VEHICLES);
        dataTypes.add(AccountDayStatConstant.USER_FINE_INCOME);
        map.put("dataType", StringUtil.getListString(dataTypes));
        List<RideOrderDaySta> accountIdDateDay = new LinkedList<>();
        if (CollUtil.isNotEmpty(accountIds)) {
            List<AccountDayStat> accountDayStats = accountDayStatDao.getByAccountIdDateWeek(map);

            List<AccountDayStat> accountDayStatRide = new LinkedList<>();
            Map<String, AccountDayStat> accountDayStatMainMap = new HashMap<>();
            Map<String, AccountDayStat> accountDayStatFineMap = new HashMap<>();
            for (AccountDayStat accountDayStat : accountDayStats) {
                if (accountDayStat.getDataType().equals(AccountDayStatConstant.CYCLING_INCOME)) {
                    accountDayStatRide.add(accountDayStat);
                } else if (accountDayStat.getDataType().equals(AccountDayStatConstant.TOTAL_NUMBER_OF_VEHICLES)) {
                    accountDayStatMainMap.put(accountDayStat.getAccountId() + "_" + accountDayStat.getDateWeek(), accountDayStat);
                } else if (accountDayStat.getDataType().equals(AccountDayStatConstant.USER_FINE_INCOME)) {
                    accountDayStatFineMap.put(accountDayStat.getAccountId() + "_" + accountDayStat.getDateWeek(), accountDayStat);
                }
            }
            accountIdDateDay = accountDayStatRide.stream().map(l -> new RideOrderDaySta(l.getAccountId(), l.getRegionId(), l.getCntTotal(), l.getSumTotal(),
                    DateTimeUtil.getYYMMDDString(String.valueOf(l.getDataDate())), l.getAddTime(), 0, 0, l.getDateWeek(), l.getDateMonth(),
                    null, null, null)).collect(Collectors.toList());
            for (RideOrderDaySta rideOrderDaySta : accountIdDateDay) {
                AccountDayStat accountDayStatMain = accountDayStatMainMap.get(rideOrderDaySta.getAccountId() + "_" + rideOrderDaySta.getDateWeek());
                if (accountDayStatMain != null) {
                    rideOrderDaySta.setMachineCount(accountDayStatMain.getSumTotal());
                } else {
                    rideOrderDaySta.setMachineCount(0L);
                }
                AccountDayStat accountDayStatFine = accountDayStatFineMap.get(rideOrderDaySta.getAccountId() + "_" + rideOrderDaySta.getDateWeek());
                if (accountDayStatFine != null) {
                    rideOrderDaySta.setOrderMoney(rideOrderDaySta.getOrderMoney() + accountDayStatFine.getSumTotal());
                } else {
                    rideOrderDaySta.setOrderMoney(rideOrderDaySta.getOrderMoney());
                }
            }
        }

        if (CollUtil.isNotEmpty(regionId)) {
            List<AccountDayStat> accountDayStats = accountDayStatDao.getByRegionIdDateWeek(map);
            accountIdDateDay.addAll(accountDayStats.stream().map(l -> new RideOrderDaySta(l.getAccountId(), l.getRegionId(), l.getCntTotal(), l.getSumTotal(), DateTimeUtil.getYYMMDDString(String.valueOf(l.getDataDate())), l.getAddTime(), 0, 0, l.getDateWeek(), l.getDateMonth(), null, null, null)).collect(Collectors.toList()));
        }
        return accountIdDateDay;
    }

    @Override
    public List<RideOrderDaySta> getByAccountIdDateMonth(List<Integer> accountIds, List<Integer> regionId, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        map.put("regionId", StringUtil.getListString(regionId));
        map.put("startDate", DateTimeUtil.getYYMMDDInteger(startDate));
        map.put("endDate", DateTimeUtil.getYYMMDDInteger(endDate));
        List<Integer> dataTypes = new LinkedList<>();
        dataTypes.add(AccountDayStatConstant.CYCLING_INCOME);
        dataTypes.add(AccountDayStatConstant.TOTAL_NUMBER_OF_VEHICLES);
        dataTypes.add(AccountDayStatConstant.USER_FINE_INCOME);
        map.put("dataType", StringUtil.getListString(dataTypes));
        List<RideOrderDaySta> accountIdDateDay = new LinkedList<>();
        if (CollectionUtils.isNotEmpty(accountIds)) {
            List<AccountDayStat> accountDayStats = accountDayStatDao.getByAccountIdDateMonth(map);

            List<AccountDayStat> accountDayStatRide = new LinkedList<>();
            Map<String, AccountDayStat> accountDayStatMainMap = new HashMap<>();
            Map<String, AccountDayStat> accountDayStatFineMap = new HashMap<>();
            for (AccountDayStat accountDayStat : accountDayStats) {
                if (accountDayStat.getDataType().equals(AccountDayStatConstant.CYCLING_INCOME)) {
                    accountDayStatRide.add(accountDayStat);
                } else if (accountDayStat.getDataType().equals(AccountDayStatConstant.TOTAL_NUMBER_OF_VEHICLES)) {
                    accountDayStatMainMap.put(accountDayStat.getAccountId() + "_" + accountDayStat.getDateMonth(), accountDayStat);
                } else if (accountDayStat.getDataType().equals(AccountDayStatConstant.USER_FINE_INCOME)) {
                    accountDayStatFineMap.put(accountDayStat.getAccountId() + "_" + accountDayStat.getDateMonth(), accountDayStat);
                }
            }
            accountIdDateDay = accountDayStatRide.stream().map(l -> new RideOrderDaySta(l.getAccountId(), l.getRegionId(), l.getCntTotal(), l.getSumTotal(), DateTimeUtil.getYYMMDDString(String.valueOf(l.getDataDate())), l.getAddTime(), 0, 0, l.getDateWeek(), l.getDateMonth(), null, null, null)).collect(Collectors.toList());
            for (RideOrderDaySta rideOrderDaySta : accountIdDateDay) {

                AccountDayStat accountDayStatMain = accountDayStatMainMap.get(rideOrderDaySta.getAccountId() + "_" + rideOrderDaySta.getDateMonth());
                if (accountDayStatMain != null) {
                    rideOrderDaySta.setMachineCount(accountDayStatMain.getSumTotal());
                } else {
                    rideOrderDaySta.setMachineCount(0L);
                }
                AccountDayStat accountDayStatFine = accountDayStatFineMap.get(rideOrderDaySta.getAccountId() + "_" + rideOrderDaySta.getDateMonth());
                if (accountDayStatFine != null) {
                    rideOrderDaySta.setOrderMoney(rideOrderDaySta.getOrderMoney() + accountDayStatFine.getSumTotal());
                } else {
                    rideOrderDaySta.setOrderMoney(rideOrderDaySta.getOrderMoney());
                }

            }
        }

        if (CollectionUtils.isNotEmpty(regionId)) {
            List<AccountDayStat> accountDayStats = accountDayStatDao.getByRegionIdDateMonth(map);
            accountIdDateDay.addAll(accountDayStats.stream().map(l -> new RideOrderDaySta(l.getAccountId(), l.getRegionId(), l.getCntTotal(), l.getSumTotal(), DateTimeUtil.getYYMMDDString(String.valueOf(l.getDataDate())), l.getAddTime(), 0, 0, l.getDateWeek(), l.getDateMonth(), null, null, null)).collect(Collectors.toList()));
        }
        return accountIdDateDay;
    }

    @Override
    public Map<String, Object> getByAccountIds(List<Integer> accountIds, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        return rideOrderDayStaDao.getByAccountIds(map);
    }

    @Override
    public Map<String, Object> getByAccountId(Integer accountId, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, accountId);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        return rideOrderDayStaDao.getByAccountId(map);
    }

    @Override
    public Map<String, Object> getByRegionId(Integer regionId, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put("regionId", regionId);
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        return rideOrderDayStaDao.getByRegionId(map);
    }

    @Override
    public List<Map<String, Object>> getGroupAccountIds(List<Integer> accountIds, String startDate, String endDate) {
        Map<String, Object> map = new HashMap<>();
        map.put(DaoParamConstant.accountId, StringUtil.getListString(accountIds));
        map.put("startDate", startDate);
        map.put("endDate", endDate);
        return rideOrderDayStaDao.getGroupAccountIds(map);
    }
}
